package dmsxl.haxibiao;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: Zhang Dongwei
 * Date: 2023/4/17 20:48
 * 两数之和
 */
public class hxb5_1 {

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++){
            int tem = target - nums[i];
            if (map.containsKey(tem)){
                res[0] = map.get(tem);
                res[1] = i;
                break;
            }
            map.put(nums[i], i);
        }
        return res;
    }
}
